package de.stefanocke.roo.wicket.form.entityselect

import de.stefanocke.roo.wicket.Stereotypes
import de.stefanocke.roo.wicket.base.WicketItdBuilder
import java.lang.reflect.Modifier

import static de.stefanocke.roo.xtend.Method.*
import static de.stefanocke.roo.xtend.MethodSignature.*
import static de.stefanocke.roo.xtend.Parameter.*
import static extension de.stefanocke.roo.xtend.RooExtensions.*

class SimpleEntitySelectItdBuilder extends WicketItdBuilder {
	override SimpleEntitySelectMetadata getMetadata(){
		super.metadata as SimpleEntitySelectMetadata
	} 

	override String baseClassStereotype(){
		Stereotypes::SIMPLE_ENTITY_SELECT	
	}
	
	override getMethods(){
		var methods = constructors 
		methods+= loadChoicesMethod
		methods+= getIdValueMethod
		methods
	}
	
	def getConstructors(){
		//TODO: Die Konstruktoren sollten sich automatisch an die der oberklasse anpassen. (MarkupProvider ja/nein?)
		newArrayList(
			constructorWithIdAndMarkupProvider,
			constructorWithIdModelAndMarkupProvider
		)
	}
	
//	@Override
//	protected List<? extends Bar> loadChoices() {
//		return Bar.findAllBars();
//	}

	def getLoadChoicesMethod(){
		method(
			signature(Modifier::PUBLIC, "java.util.List".javaType(modelClass), "loadChoices"),
			//TODO: Use persistence metadata here to determine method name
			//TODO: Move the persistence part to service bindings ?  (Where to put the select?? anonymous class in form fields?? what about customization??)
			[|'''
			return «modelClass.resolveStatic».findAll«modelClass.simpleTypeName»s();
			''']
		)
	}
//	@Override
//	protected Object getIdValue(Bar entity) {
//		return entity.getId();
//	}

	def getGetIdValueMethod(){
		method(
			signature(Modifier::PUBLIC, "java.lang.Object", "getIdValue", param(modelClass,"entity")),
			//TODO: Use persistence metadata here to determine method name
			//TODO: Use Identifiable interface instead, when available
			[|'''
			return entity.getId();
			''']
		)
	}

}